home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Subversive Component Hacks / LousyConnection / AdjustLousyConnection.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.8 KB  |  234 lines

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <Quickdraw.h>
  4. #include <Fonts.h>
  5. #include <Events.h>
  6. #include <Menus.h>
  7. #include <TextEdit.h>
  8. #include <MacWindows.h>
  9. #include <ControlDefinitions.h>
  10. #include <Dialogs.h>
  11. #include <OSUtils.h>
  12. #include <ToolUtils.h>
  13. #include <Devices.h>
  14. #include <StandardFile.h>
  15. #include <Movies.h>
  16. #include <Sound.h>
  17.  
  18. extern void createWindow(void);
  19. extern void drawWindow( WindowPtr whichWindow );
  20. extern void clickWindow( WindowPtr whichWindow, Point globalWhere );
  21.  
  22. typedef struct {
  23.     Component victim;
  24.     Fixed     dropRate;    // 0 -> never drop, fixed1 -> always drop
  25. } LousyConnectionSharedGlobalsRecord, **LousyConnectionSharedGlobalsHandle;
  26. extern LousyConnectionSharedGlobalsHandle getLCSGHandle(void);
  27. Fixed getDropRate(void);
  28. void setDropRate( Fixed dropRate );
  29.  
  30. Boolean gDone = false;
  31.  
  32. enum {
  33.     kAppleMenuID            = 128,
  34.         kAppleMenuAbout            = 1,
  35.     kFileMenuID                = 129,
  36.         kFileMenuQuit            = 1
  37.     
  38. };
  39.  
  40. static void doMenu( long menuSelection )
  41. {
  42.     short whichMenu = HiWord(menuSelection);
  43.     short whichMenuItem = LoWord(menuSelection);
  44.     
  45.     switch (whichMenu) {
  46.         case kAppleMenuID:
  47.             switch (whichMenuItem) {
  48.                 case kAppleMenuAbout:
  49.                     Alert(128, nil);
  50.                     break;
  51.  
  52.                 default:
  53.                     {
  54.                     Str255 daName;
  55.                     GetMenuItemText(GetMenuHandle(kAppleMenuID), whichMenuItem, daName);
  56.                     OpenDeskAcc(daName);
  57.                     }
  58.                     break;
  59.             }
  60.             break;
  61.  
  62.         case kFileMenuID:
  63.             switch (whichMenuItem) {
  64.                 case kFileMenuQuit:
  65.                     gDone = true;
  66.                     break;
  67.             }
  68.             break;
  69.     }
  70. }
  71.  
  72. int main( void )
  73. {
  74.     GrafPtr wmgrPort;
  75.     
  76.     InitGraf(&qd.thePort);
  77.     InitFonts();
  78.     InitWindows();
  79.     InitMenus();
  80.     TEInit();
  81.     InitDialogs(nil);
  82.     InitCursor();
  83.     
  84.     GetWMgrPort( &wmgrPort );
  85.     SetPort( wmgrPort );
  86.     EnterMovies();
  87.  
  88.     SetMenuBar(GetNewMBar(128));
  89.     AppendResMenu(GetMenuHandle(kAppleMenuID), 'DRVR');
  90.     DrawMenuBar();
  91.  
  92.     if( nil == getLCSGHandle() ) {
  93.         SInt16 itemHit;
  94.         StandardAlert( kAlertStopAlert, "\pI can’t find the LousyConnection component.  Is it registered?", "\p", nil, &itemHit );
  95.         gDone = true;
  96.     }
  97.     else {
  98.         createWindow();
  99.     }
  100.     
  101.     while (gDone == false) {
  102.         EventRecord theEvent;
  103.         WindowPtr whichWindow;
  104.         short windowPart;
  105.         
  106.         WaitNextEvent(everyEvent, &theEvent, -1, nil);
  107.         
  108.         switch (theEvent.what) {
  109.             case updateEvt:
  110.                 whichWindow = (WindowPtr)theEvent.message;
  111.                 SetPort(whichWindow);
  112.                 BeginUpdate(whichWindow);
  113.                 drawWindow(whichWindow);
  114.                 EndUpdate(whichWindow);
  115.                 break;
  116.             
  117.             case keyDown:
  118.                 if (theEvent.modifiers & cmdKey) {
  119.                     doMenu(MenuKey(theEvent.message & charCodeMask));
  120.                 }
  121.                 break;
  122.             
  123.             case mouseDown:
  124.                 windowPart = FindWindow(theEvent.where, &whichWindow);
  125.  
  126.                 switch (windowPart) {
  127.                     case inDrag:
  128.                         DragWindow(whichWindow, theEvent.where, &qd.screenBits.bounds);
  129.                         break;
  130.  
  131.                     case inGoAway:
  132.                         if (TrackGoAway(whichWindow, theEvent.where))
  133.                             gDone = true;
  134.                         break;
  135.  
  136.                     case inContent:
  137.                         if (whichWindow != FrontWindow())
  138.                         {
  139.                             SelectWindow(whichWindow);
  140.                         }
  141.                         else
  142.                         {
  143.                             clickWindow(whichWindow, theEvent.where);
  144.                         }
  145.                         break;
  146.  
  147.                     case inMenuBar:
  148.                         doMenu(MenuSelect(theEvent.where));
  149.                         break;
  150.                 }
  151.                 break;
  152.         }
  153.     }
  154.     
  155.     return 0;    
  156. }
  157.  
  158. WindowPtr window = nil;
  159. ControlHandle control = nil;
  160.  
  161. void createWindow( void )
  162. {
  163.     Rect windowBounds = { 50, 10, 90, 210 };
  164.     Rect controlBounds = { 10, 10, 30, 190 };
  165.     Fixed value;
  166.     
  167.     window = NewCWindow( nil, &windowBounds, "\pPacket Loss Rate", true, documentProc, 
  168.             (WindowPtr)-1, true, 0);
  169.     
  170.     SetPort( window );
  171.     control = NewControl( window, &controlBounds, "\p", true, 0, 0, 0x100, 
  172.             kControlSliderProc + kControlSliderLiveFeedback + kControlSliderHasTickMarks, 0 );
  173.  
  174.     value = getDropRate() >> 8;
  175.     SetControlValue( control, value );
  176. }
  177.  
  178. void drawWindow( WindowPtr whichWindow )
  179. {
  180.     if( whichWindow == window )
  181.         DrawControls( window );
  182. }
  183.  
  184. void clickWindow( WindowPtr whichWindow, Point globalWhere )
  185. {
  186.     Point localWhere = globalWhere;
  187.     Fixed value;
  188.     
  189.     GlobalToLocal( &localWhere );
  190.      
  191.     if( whichWindow == window ) {
  192.         ControlHandle whichControl = 0;
  193.         ControlPartCode part = FindControl( localWhere, window, &whichControl );
  194.         
  195.         if( whichControl == control ) {
  196.             TrackControl( control, localWhere, NULL );
  197.             
  198.             value = GetControlValue( control ) << 8;
  199.             setDropRate( value );
  200.         }
  201.     }
  202. }
  203.  
  204.  
  205. // returns false if the capture component isn't there.
  206. LousyConnectionSharedGlobalsHandle getLCSGHandle(void)
  207. {
  208.     ComponentDescription cd = { 'rtpr', 'gnrc', 'drop', 0, 0 };
  209.     Component c;
  210.     
  211.     c = FindNextComponent( 0, &cd );
  212.     if( c )
  213.         return (LousyConnectionSharedGlobalsHandle) GetComponentRefcon( c );
  214.     else
  215.         return 0;
  216. }
  217. Fixed getDropRate(void)
  218. {
  219.     LousyConnectionSharedGlobalsHandle sg = getLCSGHandle();
  220.     if( sg && *sg )
  221.         return (*sg)->dropRate;
  222.     else
  223.         return 0;
  224. }
  225. void setDropRate( Fixed dropRate )
  226. {
  227.     LousyConnectionSharedGlobalsHandle sg = getLCSGHandle();
  228.     if( sg && *sg )
  229.         (*sg)->dropRate = dropRate;
  230.     else
  231.         SysBeep(1);
  232. }
  233.  
  234.